<template>
  <div class="app-wrapper">
    <headerTitle
      :titleList="titleList"
      :hideButton="false"
      :btnTitle="'新增'"
      :btnExport="'导出名单'"
      :exportShow="false"
      icon="iconfont icon-huiyuanzhucedengluguanli"
      @handelClick="() => add()"
      @handelExport="() => exportUser()"
    />
    <div class="app-container">
      <div class="containerMain">
        <div class="search-item">
          <div class="item-list">
            <div class="item-con">
              <span>坐席编号</span>
              <el-input
                style="width: 200px"
                size="mini"
                v-model="searchform.agentId"
                placeholder="请输入坐席编号"
              ></el-input>
            </div>
            <div class="item-con">
              <span>坐席视讯号</span>
              <el-input
                style="width: 200px"
                size="mini"
                v-model="searchform.agentVideoNum"
                placeholder="请输入视讯号"
              ></el-input>
            </div>
            <div class="item-con">
              <span>坐席姓名</span>
              <el-input
                style="width: 200px"
                size="mini"
                v-model="searchform.agentName"
                placeholder="请输入坐席姓名"
              ></el-input>
            </div>
            <div class="item-con">
              <span>坐席状态</span>
              <el-select
                size="mini"
                clearable
                v-model="searchform.enable"
                placeholder="全部"
              >
                <el-option label="全部" value=""></el-option>
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="2"></el-option>
              </el-select>
            </div>
            <div class="item-con">
              <span>所属技能组</span>
              <el-select
                size="mini"
                clearable
                v-model="searchform.skillGroup"
                placeholder="全部"
              >
                <el-option
                  v-for="(item, index) in skillGroupList"
                  :key="index"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </div>
          </div>
          <div class="item-btn">
            <el-button
              class="kyeup_search_btn"
              type="primary"
              size="mini"
              @click="() => fetchData()"
              >搜索</el-button
            >
            <el-button type="default" size="mini" @click="() => reset()"
              >重置</el-button
            >
          </div>
        </div>
        <div class="container">
          <div class="div-table">
            <el-table
              :header-cell-style="headerStyle"
              :data="list"
              element-loading-text="Loading"
              fit
              size="mini"
              highlight-current-row
            >
              <el-table-column label="坐席编号">
                <template slot-scope="scope">{{ scope.row.username }}</template>
              </el-table-column>
              <el-table-column label="坐席姓名">
                <template slot-scope="scope">{{
                  scope.row.agentName
                }}</template>
              </el-table-column>
              <el-table-column label="坐席视讯号">
                <template slot-scope="scope">{{ scope.row.agentVideoNum }}</template>
              </el-table-column>
              <el-table-column label="坐席状态">
                <template slot-scope="scope">{{
                  scope.row.enable | statusFilter
                }}</template>
              </el-table-column>
              <el-table-column label="技能组">
                <template slot-scope="scope">{{
                  scope.row.skillGroupName
                }}</template>
              </el-table-column>
              <el-table-column label="创建时间">
                <template slot-scope="scope">{{
                  scope.row.createTime
                }}</template>
              </el-table-column>
              <el-table-column label="角色类型">
                <template slot-scope="scope">{{
                  scope.row.createName
                }}</template>
              </el-table-column>
              <el-table-column label="创建人">
                <template slot-scope="scope">{{
                  scope.row.createName
                }}</template>
              </el-table-column>
              <el-table-column label="更新时间">
                <template slot-scope="scope">{{
                  scope.row.updateTime
                }}</template>
              </el-table-column>
              <el-table-column label="更新人">
                <template slot-scope="scope">{{
                  scope.row.updateName
                }}</template>
              </el-table-column>
              <el-table-column label="操作" width="300px">
                <template slot-scope="scope">
                  <el-button type="text" @click="() => viewFn(scope.row)"
                    >查看</el-button
                  >
                  <el-button type="text" @click="() => editBtn(scope.row)"
                    >编辑</el-button
                  >
                  <el-button
                    type="text"
                    v-show="scope.row.enable == 1"
                    @click="() => del(scope.row,2)"
                    >禁用</el-button
                  >
                  <el-button
                    type="text"
                    v-show="scope.row.enable == 2"
                    @click="() => del(scope.row,1)"
                  >启用</el-button
                  >
                  <!-- <el-button
                    type="text"
                    v-show="scope.row.enable == 1"
                    @click="() => reloadagent(scope.row.username)"
                  ><span style="color: red">重置坐席状态</span></el-button
                  > -->
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
    </div>
    <pagination
      :exportShow="false"
      :type="'primary'"
      :pagination="pagination"
    />
  </div>
</template>

<script>
import {  fetchAllSkill} from "@/api/systemapi/skill";
import Pagination from "@/components/Pagination/index";
import HeaderTitle from "@/components/Headertitle/index";
import {
  fetchAgentList,
  fetchDel,
  reloadagent
} from "@/api/systemapi/agent";
import { pageParams } from "@/settings";

export default {
  components: {
    Pagination,
    HeaderTitle
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: "启用",
        2: "禁用",
      };
      return statusMap[status];
    },
  },
  data() {
    return {
      titleList: [{ title: "坐席管理" }],
      list:[],
      listLoading: true,
      skillGroupList: [],
      searchform: {
        agentId: "",
        agentVideoNum: "",
        agentName: "",
        skillGroup:"",
        enable:"",
        rows: "10",
        page: "1",
      },
      // 分页
      pagination: pageParams
    };
  },
  watch: {
    // 分页
    "pagination.currentPage": function () {
      this.searchform.page = this.pagination.currentPage;
      this.fetchData();
    },
    "pagination.pageSize": function () {
      this.searchform.rows = this.pagination.pageSize;
      this.searchform.page = 1;
      this.fetchData();
    },
  },
  mounted() {
    this.fetchData();
    this.fetchSkillList()
  },
  methods: {
    fetchSkillList() {
      fetchAllSkill({}).then((response) => {
        if (response.code == 0) {
          this.skillGroupList = response.data;
        } else {
          this.$message({
            type: "error",
            message: response.msg,
          });
        }
      });
    },
    exportUser() {
      downloadUserInfo({
        exportType: 1,
        ...this.searchform,
      }).then((res) => {
        let blob = new Blob([res.data], {
          type: "application/vnd.ms-excel",
        });
        let str = res.headers["content-disposition"].split(";")[1];
        // console.log(str,"str1111")
        if (str.indexOf("=") != -1) {
          var filename = decodeURI(str.slice(9));
        } else {
          var filename = decodeURI(str);
        }
        var downloadElement = document.createElement("a");
        var href = window.URL.createObjectURL(blob); //创建下载的链接
        downloadElement.href = href;
        downloadElement.download = filename; //下载后文件名
        document.body.appendChild(downloadElement);
        downloadElement.click(); //点击下载
        document.body.removeChild(downloadElement); //下载完成移除元素
        window.URL.revokeObjectURL(href); //释放掉blob对象
      });
    },

    //表头背景颜色修改
    headerStyle({ row, column, rowIndex, columnIndex }) {
      if (rowIndex == 0) {
        return "background:#F5F6FB;color:#666";
      }
    },

    // 重置搜索，刷新列表
    reset() {
      (this.searchform = {
        agentId: "",
        agentVideoNum: "",
        agentName: "",
        skillGroup:"",
        enable:"",
        rows: "10",
        page: "1",
      }),
        this.fetchData();
    },
    fetchData() {
      fetchAgentList({...this.searchform}).then((response) => {
        if (response.code == 0) {
          this.list = response.data.list;
          this.pagination.total = response.data.records;
          console.log(response.data.list)
          console.log(this.list)
        } else {
          this.$message({
            type: "error",
            message: response.msg,
          });
        }
      });
    },
    fetchDetailData(id) {
      fetchDetail({id: id}).then((response) => {
        if (response.code == 0) {
          this.list = response.data;
          this.pagination.total = response.data.records;
          this.$router.push({ path: "/" });
        } else {
          this.$message({
            type: "error",
            message: response.msg,
          });
        }
      });
    },
    reloadagent(agentId){
      this.$confirm("是否确定重置坐席状态", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        reloadagent({agentId:agentId}).then((response) => {
          if (response.code === 0) {
            this.fetchData();
            this.$message({
              message: "重置成功",
              type: "success",
            });
          } else {
            this.$message({
              message: response.msg,
              type: "error",
            });
          }
        });
      });
    },
    // 删除
    del(data,enable) {
      this.$confirm(enable == 1 ? "是否确定启用?":"是否确定禁用?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        fetchDel({id:data.id,skillId:data.skillGroup,enable:enable}).then((response) => {
          if (response.code === 0) {
            this.fetchData();
            this.$message({
              message: enable == 1 ? "启用成功":"禁用成功",
              type: "success",
            });
          } else {
            this.$message({
              message: response.msg,
              type: "error",
            });
          }
        });
      });
    },
    recover(data) {
      fetchRecover({ id: data.id, userType: "3" }).then((response) => {
        if (response.code === 0) {
          this.fetchData();
          this.$message({
            message: "恢复成功",
            type: "success",
          });
        } else {
          this.$message({
            message: response.msg,
            type: "error",
          });
        }
      });
    },

    // 新增
    add(data) {
      this.$router.push({
        path: "/system/agent/detail",
        query: { type: 3},
      });
    },
    editBtn(data) {
      this.$router.push({
        path: "/system/agent/detail",
        query: { type: "2", id: data.id},
      });
    },
    viewFn(data) {
        this.$router.push({
        path: "/system/agent/detail",
        query: { type: "1", id: data.id},
      });
    },
    handelExport() {
      this.dialogVisible = true;
    },
  },
};
</script>
<style lang="scss">
.tree-con {
  .el-tree-node__content {
    margin-bottom: 20px;
  }
}
</style>
<style lang="scss" scoped>
.containerMain {
  .search-item {
    padding: 20px 10px;
    font-size: 14px;
    color: #666;
    display: flex;
    justify-content: space-between;
    .item-list {
      display: flex;
      flex-wrap: wrap;
    }
    .item-con {
      display: flex;
      margin-right: 30px;
      margin-top: 20px;

      span {
        display: inline-block;
        line-height: 28px;
        margin-right: 10px;
      }
    }
    .item {
      width: 180px;
    }
  }
  .container {
    display: flex;
    flex-direction: row;
    .div-tree {
      width: 250px;
    }
    .div-table {
      flex: 1;
      width: 100%;
      overflow: hidden;
      .el-table {
        height: 100%;
        width: 100%;
        overflow: auto;
      }
    }
    .el-row {
      height: 300px;
      .el-col {
        height: 100%;
        .div-tree {
          height: 90%;
          .el-tree {
            height: 100%;
            overflow-y: auto;
          }
        }
      }
    }
  }
  .item-btn {
    margin-top: 20px;
    flex: 0 0 140px;
  }
}
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>
